package com.yoo.ons

import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.module_base.utils.log
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request
import com.yoo.ons.util.appContext

/**
 * app级别viewmodel
 * 1、会员消费
 */
class AppUserViewModel : BaseVMViewModel() {

    //获取fc随机msg
    fun postFlashChatMsg(callback: (String) -> Unit) {
        val listener = object : HttpRequestListener<String>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
                params["code"] = 5
            }

            override fun success(t: String?) {
            }
        }
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postFlashChatMsg(map)?.requestObserver(
                    NetworkObserver<String>().startRequest {
                    }.success {
                        log("AppUserViewModel postFlashChatMsg success $it")
                        callback.invoke(it ?: "")

                    }.error {
                        log("AppUserViewModel postFlashChatMsg error")
                        //listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }
        })
    }

    //会员消费
    fun postReduceBenefits(listener: HttpRequestListener<String>,callback: () -> Unit) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postReduceBenefits(map)?.requestObserver(
                    NetworkObserver<String>().startRequest {
                    }.success {
                        log("AppUserViewModel postReduceBenefits success")
                        callback.invoke()
                    }.error {
                        log("AppUserViewModel postReduceBenefits error")
                        //listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }
        })
    }

    companion object {
        fun get(): AppUserViewModel {
            return (appContext as App).getAppViewModelProvider().get(AppUserViewModel::class.java)
        }
    }
}